﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Web;
using System.Web.SessionState;

namespace DFT.AJAX
{
    /// <summary>
    /// Summary description for ImageHandler
    /// </summary>
    public class ImageHandler : IHttpHandler, IRequiresSessionState
    {
        public void ProcessRequest(HttpContext context)
        {
            String f = context.Request.QueryString.Get("f");
            String s = context.Request.QueryString.Get("s");
            if (!String.IsNullOrEmpty(f))
            {
                HttpPostedFile file = context.Request.Files[f];
                if (file == null)
                {
                    HttpContext.Current.ApplicationInstance.CompleteRequest();
                }
                else
                {
                    List<String> keys = new List<String>();
                    foreach (String key in context.Session.Keys)
                    {
                        if (key.StartsWith("image"))
                        {
                            keys.Add(key);
                        }
                    }
                    foreach (String key in keys)
                    {
                        context.Session.Remove(key);
                    }
                    Image image = Bitmap.FromStream(file.InputStream);
                    context.Session["image"] = image;
                    context.Session["imageContentType"] = context.Request.Files[0].ContentType;
                    context.Response.Flush();
                    HttpContext.Current.ApplicationInstance.CompleteRequest();
                }
            }
            else if (!String.IsNullOrEmpty(s))
            {
                String imageName = "image";
                String imageType = "imageContentType";
                if (context.Session[imageType] == null || context.Session[imageName] == null)
                {
                    HttpContext.Current.ApplicationInstance.CompleteRequest();
                    if (context.Session[imageType] != null)
                    {
                        context.Session.Remove(imageType);
                    }
                    if (context.Session[imageName] != null)
                    {
                        context.Session.Remove(imageName);
                    }
                    if (context.Session["imageByteArr"] != null)
                    {
                        context.Session.Remove("imageByteArr");
                    }
                }
                else
                {
                    using (Image image = (Image)context.Session[imageName])
                    {
                        context.Response.Clear();
                        context.Response.ClearHeaders();
                        String type = context.Session[imageType].ToString().ToLower();
                        ImageFormat format = ImageFormat.Gif;
                        Boolean isValid = true;
                        if (type.Contains("bmp"))
                        {
                            format = ImageFormat.Bmp;
                        }
                        else if (type.Contains("jpg") || type.Contains("jpeg"))
                        {
                            format = ImageFormat.Jpeg;
                        }
                        else if (type.Contains("png"))
                        {
                            format = ImageFormat.Png;
                        }
                        else if (type.Contains("gif"))
                        {
                            format = ImageFormat.Gif;
                        }
                        else if (type.Contains("wmf"))
                        {
                            format = ImageFormat.Wmf;
                        }
                        else if (type.Contains("tiff") || type.Contains("tif"))
                        {
                            format = ImageFormat.Tiff;
                        }
                        else if (type.Contains("exif"))
                        {
                            format = ImageFormat.Exif;
                        }
                        else if (type.Contains("ico"))
                        {
                            format = ImageFormat.Icon;
                        }
                        else
                        {
                            isValid = false;
                        }
                        if (isValid)
                        {
                            using (MemoryStream stream = new MemoryStream())
                            {
                                image.Save(stream, format);
                                context.Session["imageByteArr"] = stream.ToArray(); 
                                context.Response.BinaryWrite(stream.ToArray());
                                context.Response.ContentType = type;

                            }
                        }
                        //context.Session.Remove(imageName);
                        //context.Session.Remove(imageType);
                        HttpContext.Current.ApplicationInstance.CompleteRequest();
                    }
                }
            }
        }
        public Boolean IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}